/*
 * common.c
 *
 *  Created on: 14-12-2011
 *      Author: radek
 */

#include "common.h"
#include <assert.h>
#include <string.h>
#include <math.h>

// zwraca liczbe utworzonych wiadomosci
int create_fake_response(char *data, size_t data_size, struct monitor_response *responses) {
	int i, j, len;
	int seed = time(NULL);
	srand(seed);

	int msg_num = 1;

	if (data_size >= MAX_PAYLOAD) {
		msg_num = data_size % (MAX_PAYLOAD - 1) == 0 ? data_size / (MAX_PAYLOAD-1)
				: (int) (((float) data_size / (float) (MAX_PAYLOAD-1)) + 1.0);
		len = MAX_PAYLOAD-1;
	}

	int mid = rand() % 200;
	int type = rand() % 100;
	j = 0;
	for(i = 0; i < msg_num; i++) {
		if(i == msg_num-1)
			len = data_size - (MAX_PAYLOAD-1)*(msg_num-1);

		struct monitor_response *response = responses+i;
		response->mtype = type;
		response->message_id = mid;
		memcpy(response->payload, data+i*(MAX_PAYLOAD-1), len);
		response->payload[len] = '\0';
	}
	return msg_num;
}

size_t create_fake_data(char **buff) {
	assert(*buff == NULL);
	int seed = time(NULL);
	size_t i;
	srand(seed);

	size_t size = MAX_PAYLOAD + (rand() % 3*MAX_PAYLOAD);


	*buff = malloc(size);
	for(i = 0; i < size; i++) {
		(*buff)[i] = !(i % 31) ? 32 : (rand() % 26) + 65;
	}
	(*buff)[size] = '\0';
	return size;
}
